#encoding:UTF-8
import re
import mylib
import pythonlibSB


def get_hot_link():
    try:
        results = []
        hot_10_link = []
        content = mylib.get_content('http://vnexpress.net/')
        hot_content = pythonlibSB.s_string(content,'id="topnews"', 'div')
        
        hot_10_content = pythonlibSB.s_string(hot_content,'class="hotnews-content"','div')
#        print hot_10_content
        reg = re.compile('(<a)(.*?)(class="link-topnews")(.*?)(href=")(.*?)(")')
        if reg.search(hot_10_content):
            url = reg.search(hot_10_content).group(6)
            hotlink = 'http://vnexpress.net'+url if url.find('http://')==-1 else url
            hot_10_link.append(hotlink)
        results.append(hot_10_link)
        
        hot_9_link = []
        hot_9_content = pythonlibSB.s_string(hot_content,'class="top3"','div')
        for i in re.finditer('(<a)(.*?)(href=")(.*?)("><img)',hot_9_content):
            hot9link = ('http://vnexpress.net' + i.group(4)) \
                        if i.group(4).find('http://') == -1 else i.group(4)
            hot_9_link.append(hot9link)
        results.append(hot_9_link)
        
        
        hot_8_link = []
        hot_8_content = pythonlibSB.s_string(hot_content,'class="toplist"','div')
        for i in re.finditer('(<a)(.*?)(href=")(.*?)(")',hot_8_content):
            hot8link = ('http://vnexpress.net' + i.group(4)) \
                        if i.group(4).find('http://') == -1 else i.group(4)
            hot_8_link.append(hot8link)
        results.append(hot_8_link)
        
    
        hot_7_link = []
       
        
        results.append(hot_7_link)
        
        return results
    except:
        return []


if __name__ == "__main__":
    hot_link = get_hot_link()
    print hot_link
    hot8 = hot_link[3]
    print hot8
    print len(hot8)
    
